<?php
/**
 * @Author: Marte
 * @Date:   2017-08-04 14:21:17
 * @Last Modified by:   Marte
 * @Last Modified time: 2017-08-20 14:16:41
 */
session_start();
// echo $_SESSION['code'];
header('Content-Type:text/html;charset=utf-8');
include "../config_connect.php";//导入数据库
require '../../common/register.func.php';//引入函数库
include_once("../../include/smtp.class.php");//邮件发送类
if(!isset($_POST["submit"])){
      exit('非法访问!');
  }

$_username=$_POST['username'];//接受表单内容
$_password=$_POST['password'];
$_prepassword=$_POST['prepassword'];
$email = $_POST['email'];
$yzm=$_POST['yzm'];
// echo $yzm;
// 判断后传入数据库
_check_username($_username);
_check_password($_password);
_check_email($email);
_check_passwd($_password,$_prepassword);

$regtime=time();
$username=md5(trim($_POST['username']));//去掉前后的空格后，在加密
$password=md5(trim($_POST['password']));
$token=md5($username.$password.$regtime);//激活码

if(!($_SESSION['code']==$yzm))
{
  //如果验证码不正确，
  echo "<script>location='../../zc.php?error=1'</script>";
}
else
{
  $_sql1="select * from userregisters";

  $_result=$_mysql->query($_sql1);
  //得到关联数组
  while ( $_assoc=$_result->fetch_assoc()) {
        if($_assoc['username']==$_username||$_assoc['email']==$email)
               {
                    echo "<script>location='../../zc.php?error=2'</script>";
               }
  }
  //当没有与数据库中的已有账户冲突时，就添加到数据库中
    $sql = "INSERT INTO userregisters (username, password, email,token,status)
    VALUES ('$_username', '$_password', '$email','$token',0)";
    if ($_mysql->query($sql) == TRUE) {
      echo "<script>alert('恭喜您注册成功！');location='../../zc.php?true=1';</script>";
    } else {
      echo "err".$_mysql->error;
    // echo "<script>alert('很抱歉注册失败了，请您再次注册！');location='../../zc.php'</script>";
    }
    $_mysql->close();
}



?>